package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CourseDO;
import com.ctms.system.domain.CourseScoreDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.CourseService;
import com.ctms.system.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/teacherStu")
@Controller
public class TeacherStuController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    CourseService courseService;

    @GetMapping("")
    public String teacherStu(){
        return "sys/teacher/teacherStu";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> param){
        UserDO userDO = ShiroUtils.getUser();
        int userId = Integer.parseInt(Long.toString(userDO.getUserId()));
        param.put("loginId",userId);
        List<UserDO> userDOS = teacherService.selectTeacherStu(param);
        for (int i=0;i<userDOS.size();i++){
            String courseName = userDOS.get(i).getCourseName();
            //通过courseName查找courseId
            CourseDO courseDO = courseService.selectCourseByCid(courseName);
            int courseId = courseDO.getCid();
            int user = Integer.parseInt(Long.toString(userDOS.get(i).getUserId()));
            //通过courseId、user查询对应的成绩
            CourseScoreDO courseScoreDO = teacherService.selectScoreByCidUserId(courseId,user);
            int score = courseScoreDO.getScore();
            userDOS.get(i).setScore(score);
        }
        Query query = new Query(param);
        int total = teacherService.countTeacherStu(query);
        PageUtils pageUtils = new PageUtils(userDOS,total);
        return pageUtils;
    }
}
